<?php

class AdvertiseModel extends BpfModel
{

  public function createAdvertise($form)
  {
    return Advertise::create($form);
  }

  public function getAdvertise($advertiseid, $type = 'advertises.advertise_id')
  {
    static $list = [];
    $cacheid = $advertiseid . $type;
    if (empty($list[$cacheid])) {
      $mysqlModel = $this->getModel('mysql');
      $advertise = $mysqlModel->getSqlBuilder()
        ->from('advertises')
        ->where($type, $advertiseid)
        ->query()->row('Advertise');
      $list[$cacheid] = $advertise;
    }
    return $list[$cacheid];
  }

  private function _setAdvertisesFilter($query, $filter)
  {
    $query->from('advertises')
      ->join('users', 'advertises.user_id = users.user_id');
    if (isset($filter)) {
      if (!empty($filter['search'])) {
        $search = '%' . $filter['search'] . '%';
        $query->WhereGroupStart();
        $query->Where('users.username LIKE', $search);
        $query->orWhere('advertises.name LIKE', $search);
        $query->whereGroupEnd();
      }
      if (isset($filter['status']) && is_numeric($filter['status'])) {
        $query->Where('advertises.status', $filter['status']);
      }
    }
  }

  public function getAdvertisesCount($filter = null)
  {
    $mysqlModel = $this->getModel('mysql');
    $query = $mysqlModel->getSqlBuilder()
        ->select('COUNT(0)');
    $this->_setAdvertisesFilter($query, $filter);
    return $query->query()->field();
  }

  public function getAdvertisesList($filter = null, $page = 1, $rows = 10)
  {
    $mysqlModel = $this->getModel('mysql');
    $query = $mysqlModel->getSqlBuilder()
        ->select('advertises.*, users.nickname')
        ->limitPage($rows, $page);
    $this->_setAdvertisesFilter($query, $filter);
    if (isset($filter['created ASC']) ) {
      $query->orderby('advertises.created asc');
    } else {
      $query->orderby('advertises.created desc');
    }
    return $query->query()->all('Advertise');
  }
}


class Advertise extends BpfObject
{
  const IS_OFFLINE = 0;
  const IS_ONLINE = 1;

  public static function create($form)
  {
    $advertiseid = BpfObject::getModel('mysql')->getSqlBuilder()->insert('advertises', [
      'user_id' => intval($form['user_id']),
      'name' => trim($form['name']),
      'image' => trim($form['image']),
      'status' => Advertise::IS_OFFLINE,
      'created' => REQUEST_TIME,
      'updated' => REQUEST_TIME
    ])->query()->insertId();
    return BpfObject::getModel('advertise')->getAdvertise($advertiseid);
  }

  public function getId()
  {
    return $this->advertise_id;
  }

  public function delete()
  {
    return $this->getModel('mysql')->getSqlBuilder()->delete('advertises', [
      'advertise_id' => $this->advertise_id
    ])->query()->affectedRows();
  }


  public function update()
  {
    return $this->getModel('mysql')->getSqlBuilder()->update('advertises', [
      'status' => intval($this->status),
      'updated' => REQUEST_TIME
    ], ['advertise_id' => $this->advertise_id])->query()->affectedRows();
  }

}
